@extends('admin.public.header')
@section('title','售后订单列表')

@section('listsearch')
    <fieldset class="table-search-fieldset" style="display:black">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane form-search" action="" id="searchFrom">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户昵称</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="请输入用户昵称" name="nickname" id="nickname" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">收货人姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="请输入收货人姓名" name="name" id="name" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">收货人电话</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="请输入收货人电话" name="mobile" id="mobile" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单编号</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="请输入订单编号" name="order_no" id="order_no" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">起始日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="begin_time" id="begin_time" placeholder="请选择起始日期" autocomplete="off" value="" class="layui-input">
                        </div>
                        <label class="layui-form-label">截止日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="end_time" id="end_time" placeholder="请选择截止日期" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>
                    <!--
                    <div class="layui-inline">
                        <label class="layui-form-label">售后订单状态</label>
                        <div class="layui-input-inline">
                            <select name="refund_status" id="refund_status" lay-search>
                                <option value="">选择或输入</option>
                                <option value="0">待处理</option>
                                <option value="1">已处理</option>
                            </select>
                        </div>
                    </div>
                    -->
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-sm new-day" data-value="7">近7天</button>
                        <button class="layui-btn layui-btn-sm new-day" data-value="30">近30天</button>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-sm layui-btn-normal"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-primary data-reset-btn" lay-submit lay-filter="data-reset-btn" >重置 </button>
                    </div>
                </div>
            </form>
        </div>
    </fieldset>
@endsection

@section('listcontent')
<!-- 同意退货 -->
<div class="layui-row" id="agree_goods_form" style="display:none;">
    <div class="layui-col-md11" style="margin-top: 20px;">
        <form class="layui-form">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="refund_type" value="2">
            <input type="hidden" name="action" value="1">
            <input type="hidden" name="refund" value="2">
            <div class="layui-form-item" style="margin-left: 20px;">确认同意退货？同意后由客户填写退货快递单号，确认收到货后退款金额返还给客户。</div>
            <div class="layui-form-item">
                <label class="layui-form-label">退款金额</label>
                <div class="layui-input-block">
                    <input type="number" id="refund_price" name="refund_price" value="" lay-verify="required" lay-reqtext="退款金额不能为空" class="layui-input">
                    <tip>请输入退款金额<span id="refund_price_text"></span></tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">收货地址（用户参考填写）</label>
                <div class="layui-input-block">
                    <select name="refund_address_id" lay-verify="" lay-search>
                        @foreach($address_list as $key => $v)
                        <option value="{{ $v['id'] }}">{{ $v['address'] }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">提 交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 同意退款 -->
<div class="layui-row" id="refund_only_form" style="display:none;">
    <div class="layui-col-md11" style="margin-top: 20px;">
        <form class="layui-form">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="refund_type" value="1">
            <input type="hidden" name="action" value="1">
            <div class="layui-form-item">
                <label class="layui-form-label">退款金额</label>
                <div class="layui-input-block">
                    <input type="number" id="refund_price" name="refund_price" value="" lay-verify="required" lay-reqtext="退款金额不能为空" class="layui-input">
                    <tip>请输入退款金额<span id="refund_price_text"></span></tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">退款方式</label>
                <div class="layui-input-block">
                    @foreach($OrderRefundModel->getRefundPayType() as $k=>$v)
                        <input type="radio" name="refund_pay_type" value="{{$k}}" title="{{$v}}" @if($k == 1) checked="" @endif />
                    @endforeach
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">提 交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 确认收货 -->
<div class="layui-row" id="confirm_form" style="display:none;">
    <div class="layui-col-md11" style="margin-top: 20px;">
        <form class="layui-form">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="refund_type" value="1">
            <input type="hidden" name="action" value="1">
            <input type="hidden" name="refund" value="1">
            <input type="hidden" name="confirm" value="1">
            <div class="layui-form-item" style="margin-left: 30px;" id="confirm"></div>
            <div class="layui-form-item">
                <label class="layui-form-label">退款金额</label>
                <div class="layui-input-block">
                    <input type="number" id="refund_price" name="refund_price" value="" lay-verify="required" lay-reqtext="退款金额不能为空" class="layui-input">
                    <tip>请输入退款金额<span id="refund_price_text"></span></tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">退款方式</label>
                <div class="layui-input-block">
                    @foreach($OrderRefundModel->getRefundPayType() as $k=>$v)
                        <input type="radio" name="refund_pay_type" value="{{$k}}" title="{{$v}}" @if($k == 1) checked="" @endif />
                    @endforeach
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">提 交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 同意换货 -->
<div class="layui-row" id="agree_exchange_form" style="display:none;">
    <div class="layui-col-md11" style="margin-top: 20px;">
        <form class="layui-form">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="refund_type" value="3">
            <input type="hidden" name="action" value="1">
            <div class="layui-form-item">
                <label class="layui-form-label">收货地址（用户参考填写）</label>
                <div class="layui-input-block">
                    <select name="refund_address_id" lay-verify="" lay-search>
                        @foreach($address_list as $key => $v)
                        <option value="{{ $v['id'] }}">{{ $v['address'] }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">提 交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 拒绝退款 拒绝退款退货 拒绝换货 -->
<div class="layui-row" id="refuse_form" style="display:none;">
    <div class="layui-col-md11" style="margin-top: 20px;">
        <form class="layui-form">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="action" value="2">
            <div class="layui-form-item">
                <label class="layui-form-label">拒绝原因：</label>
                <div class="layui-input-block">
                    <textarea placeholder="请描述拒绝原因..." id="refuse_desc" lay-verify="required" lay-reqtext="拒绝原因不能为空" class="layui-textarea" name="refuse_desc"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveRefuseBtn" lay-submit lay-filter="saveRefuseBtn">提 交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<table class="layui-hide" id="tableList" lay-filter="tableList"></table>
<!-- 表头左侧按钮 -->
<script type="text/html" id="toolbarColumn">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layuimini-btn-primary" onclick="window.location.reload();" ><i class="layui-icon layui-icon-refresh-3"></i></button>
        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-black" data-value="0" lay-event="screen"><i class="layui-icon"></i>待处理</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-blue" data-value="1" lay-event="screen"><i class="layui-icon"></i>已处理</button>
    </div>
</script>
<!-- 操作按钮 -->
<script type="text/html" id="barOperate">
    
</script>
@endsection

@section('listscript')
<script type="text/javascript">
    layui.use(['form','table','miniTab','laydate','laytpl'], function(){
        var table = layui.table, $=layui.jquery, form = layui.form , miniTab = layui.miniTab, laydate = layui.laydate , laytpl = layui.laytpl;
        miniTab.listen();
        
        //日期
        laydate.render({
            elem: '#begin_time'
            ,theme: '#393D49'
            ,festival: true //显示节日
		    ,istime: false
            //,type: 'datetime'
            ,choose: function(datas){ //选择日期完毕的回调
			    compare_time($('#begin_time').val(),$('#end_time').val());
		    }
        });
        laydate.render({
            elem: '#end_time'
            ,theme: '#393D49'
            ,festival: true //显示节日
		    ,istime: false
            //,type: 'datetime'
            ,choose: function(datas){ //选择日期完毕的回调
			    compare_time($('#begin_time').val(),$('#end_time').val());
		    }
        });
        // 渲染表格
        var tableIns = table.render({
                elem: '#tableList',
                url:'/admin/order_refund/ajaxList',
                parseData: function(res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.count, //解析数据长度
                        "data": res.data.list //解析数据列表
                    }
                },
                cellMinWidth: 50,//全局定义常规单元格的最小宽度
                toolbar: '#toolbarColumn',//开启头部工具栏，并为其绑定左侧模板
                defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '搜索',
                    layEvent: 'TABLE_SEARCH',
                    icon: 'layui-icon-search'
                }],
            title: '订单列表',
            cols: [[
                {type: 'checkbox', align: 'center'},
                {field:'id', title:'ID', width:50, align: 'center', unresize: true},
                {field:'order_no', title:'订单号', align: 'center', unresize: true},
                {field:'nickname', title:'下单用户', align: 'center', templet: function(res){
                    return '<img style="width:30px;height:30px;" src="'+res.avatar+'"><span style="margin-left: 10px;">'+res.nickname+'</span>'
                }},
                {field:'order_detail', title:'商品信息', align: 'center', templet: function(res){
                    var html = '';
                    for ( var k in res.order_detail) {
                        html += '<div style="width: 100%;height:60px;"><img style="width:60px;height:60px;display: block;float: left;" src="'+res.order_detail[k].goods_pic+'"><span style="margin-left: 10px;">'+res.order_detail[k].goods_name+'</span><br/><span style="margin-left: 10px;">规格：【'+res.order_detail[k].goods_attr[0].attr_group_name+'-'+res.order_detail[k].goods_attr[0].attr_name+'】</span><span style="margin-left: 10px;color:#1e9fff">金额：'+res.order_detail[k].pay_price+'元</span></div>'
                    }
                    return html;
                }},
                {field:'name', title:'收货人信息', align: 'center', templet: function(res){
                    return '<span>'+res.name+'【'+res.mobile+'】</span><br/>'
                            +'<span>'+res.address+'</span>'
                }},
                {field:'refund_type_name', title:'售后类型', align: 'center', templet: function(res){
                    return '<span style="color:#1E9FFF;">'+res.refund_type_name+'</span>'
                }},
                {field:'refund_price', title:'退款金额', align: 'center', templet: function(res){
                    return '<span style="color:#FF5722;">退款金额：'+res.refund_price+'</span><br/>'
                            +'<span>退款理由：'+res.user_desc+'</span>'
                }},
                {field:'refund_status_name', title:'售后状态', align: 'center', templet: function(res){
                    return '<span style="color:#009688;">'+res.refund_status_name+'</span>'
                }},
                {field:'created_at', title:'申请售后时间', align: 'center'},
                {title:'操作', width:180, align: 'center', templet: function(res){
                    if(res.refund_status == 0){
                        if(res.refund_type == 3){
                            return '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="agree_refund">同意退款</a>'
                            +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="refuse">拒绝退款</a><br/>'
                        +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="detail"><i class="layui-icon"></i>详情</a>'
                        }
                        // else
                        if(res.refund_type == 1){
                            if(res.is_agree == 1){
                                if(res.is_user_send == 1){
                                    return '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="confirm">确认收货</a>'
                                    +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="detail"><i class="layui-icon"></i>详情</a>'
                                }else{
                                    return ''
                                }
                            }else{
                                return '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="agree_goods">同意退货</a>'
                                +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="refuse">拒绝退货</a><br/>'
                                +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="detail"><i class="layui-icon"></i>详情</a>'
                            }
                        }else if(res.refund_type == 2){
                            return '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="agree_exchange">同意换货</a>'
                            +'<a data-value="拒绝换货" class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="refuse">拒绝换货</a><br/>'
                            +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="detail"><i class="layui-icon"></i>详情</a>'
                        }
                    }else{ 
                        return '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="delete"><i class="layui-icon"></i>删除</a>'
                        +'<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" lay-event="detail"><i class="layui-icon"></i>详情</a>'
                    }        
                }}
            ]],
            id: 'listReload',
            limits: [15, 20, 30, 50, 100,200],
            limit: 15,
            page: true,
            text: {
                none: '抱歉！暂无数据~' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });
        
        //头工具栏事件
        table.on('toolbar(tableList)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var ids = [];
            var data = checkStatus.data;
            for (var i=0;i<data.length;i++){
                ids.push(data[i].id);
            }
            switch(obj.event){
                case 'batch_delete':
                    layer.confirm('确定将回收站的数据清空吗？', {
                    title : "操作确认",
                    skin: 'layui-layer-lan'
                    },function(index){
                        $.ajax({
                            url:'/admin/order/batchDelete',
                            type:'post',
                            data:{},
                            dataType:"JSON",
                            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                            success:function(data){
                                if(data.code == 0){
                                    layer.msg(data.message,{icon: 1,time:1500},function(){
                                        setTimeout('window.location.reload()',500);
                                    });
                                }
                                else{
                                    layer.msg(data.message,{icon: 2});
                                }
                            },
                            error:function(e){
                                layer.msg(data.message,{icon: 2});
                            },

                        });
                    });
                case 'screen':
                    var value = $(this).data('value');
                    var nickname = $("#nickname").val();
                    var name = $("#name").val();
                    var mobile = $("#mobile").val();
                    var order_no = $("#order_no").val();
                    var begin_time = $("#begin_time").val();
                    var end_time = $("#end_time").val();
                    $.ajax({
                        url:'/admin/order_refund/ajaxList',
                        type:'get',
                        data:{'refund_status':value,'page': 1,'limit': 15},
                        dataType:"JSON",
                        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                        success:function(data){
                            if(data.code == 0){
                                tableIns.reload({
                                    method: 'get'
                                    , where: {
                                        'refund_status': value,
                                        'nickname': nickname,
                                        'name': name,
                                        'mobile': mobile,
                                        'order_no':order_no,
                                        'begin_time':begin_time,
                                        'end_time':end_time,
                                    }
                                    , page: {
                                        curr: 1
                                    }
                                });
                            }else{
                                layer.msg(data.message,{icon: 2});
                            }
                        },
                        error:function(e){
                            layer.msg(data.message,{icon: 2});
                        },

                    });
                break;
                
                //自定义头工具栏右侧图标 - 提示
                case 'TABLE_SEARCH':
                    var display = $(".table-search-fieldset").css("display"); //获取标签的display属性
                    if(display == 'none'){
                        $(".table-search-fieldset").show();
                    }else{
                        $(".table-search-fieldset").hide();
                    }
                break;
               
            };
        });
        
        //监听行工具事件
        table.on('tool(tableList)', function(obj){
            var data = obj.data;
            var id = data.id;
            var refund_type = data.refund_type;
            var refund_title = '';
            
            if(obj.event === 'agree_goods'){
                //同意退款
                $("input[name=id]").val(id);
                $("input[name=refund_price]").attr('value',data.refund_price);
                
                $("#refund_price_text").text('（最多'+data.refund_price+'元）')
                var index = layer.open({
                    title: '提示',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['30%', '45%'],
                    skin: 'layui-layer-lan',
                    content: $("#agree_goods_form").html(),
                    success: function (){
                        form.render();  //更新渲染表单
                    },
                });
            }else if(obj.event === 'agree_refund'){
                //同意退款
                $("input[name=id]").val(id);
                $("input[name=refund_price]").attr('value',data.refund_price);
                
                $("#refund_price_text").text('（最多'+data.refund_price+'元）')
                var index = layer.open({
                    title: '提示',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['30%', '40%'],
                    skin: 'layui-layer-lan',
                    content: $("#refund_only_form").html(),
                    success: function (){
                        form.render();  //更新渲染表单
                    },
                });
            }else if(obj.event === 'agree_exchange'){
                //同意换货
                $("input[name=id]").val(id);
                var index = layer.open({
                    title: '提示',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['30%', '40%'],
                    skin: 'layui-layer-lan',
                    content: $("#agree_exchange_form").html(),
                    success: function (){
                        form.render();  //更新渲染表单
                    },
                });
            }else if(obj.event === 'confirm'){
                //确认收货
                $("input[name=id]").val(id);
                $("input[name=confirm]").val(1);
                $("input[name=refund_price]").attr('value',data.refund_price);
                $("#confirm").text('确认已收到货？确认收货后退款金额'+data.refund_price+'元将直接返还给用户！')
                $(".confirm").hide();
                $("#refund_price_text").text('（最多'+data.refund_price+'元）')
                var index = layer.open({
                    title: '提示',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['30%', '40%'],
                    skin: 'layui-layer-lan',
                    content: $("#confirm_form").html(),
                    success: function (){
                        form.render();  //更新渲染表单
                    },
                });
            }else if(obj.event === 'refuse'){//拒绝申请（退款，退款退货，换货）
                if(refund_type == 1){
                    refund_title = '拒绝退款';
                }else if(refund_type == 2){
                    refund_title = '拒绝退货';
                }else if(refund_type == 3){
                    refund_title = '拒绝换货';
                }
                //同意换货
                $("input[name=id]").val(id);
                var index = layer.open({
                    title: refund_title,
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['30%', '40%'],
                    skin: 'layui-layer-lan',
                    content: $("#refuse_form").html(),
                });
            }else if(obj.event === 'delete'){//删除订单
                layer.confirm('确定删除该订单吗？', {
                    title : "操作确认",
                    skin: 'layui-layer-lan'
                },function(index){
                    $.ajax({
                        url:'/admin/order_refund/delete',
                        type:'post',
                        data:{'id':id},
                        dataType:"JSON",
                        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                        success:function(data){
                            if(data.code == 0){
                                layer.msg(data.message,{icon: 1,time:1500},function(){
                                    setTimeout('window.location.reload()',500);
                                });
                            }
                            else{
                                layer.msg(data.message,{icon: 2});
                            }
                        },
                        error:function(e){
                            layer.msg(data.message,{icon: 2});
                        },
                        
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'detail'){//订单详情
                var index = layer.open({
                    title: '售后订单详情',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    skin: 'layui-layer-lan',
                    content: '/admin/order_refund/detail?id='+id,
                });
            }
        });

        //同意处理提交
        form.on('submit(saveBtn)', function(data){
            var loading = layer.msg('加载中..', {icon: 16,shade: 0.3,time: false});
            $.ajax({
                url:'/admin/order_refund/refundHandle',
                type:'post',
                data:data.field,
                dataType:"JSON",
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success:function(res){
                    layer.close(loading);  //返回数据关闭loading
                    if(res.code == 0){
                        $("#saveBtn").removeClass("layui-btn-normal").addClass("layui-btn-disabled");
                        $("#saveBtn").attr("disabled","true");
                        layer.msg(res.message,{icon: 1},function(){
                            setTimeout('location.reload()',500);
                        });
                        
                    }
                    else{
                        layer.msg(res.message,{icon: 2});
                    }
                },
                error:function(e){
                    layer.msg("提交失败",{icon: 2});
                },
                
            });
            return false;
        });

        //拒绝处理提交
        form.on('submit(saveRefuseBtn)', function(data){
            var loading = layer.msg('加载中..', {icon: 16,shade: 0.3,time: false});
            $.ajax({
                url:'/admin/order_refund/refundHandle',
                type:'post',
                data:data.field,
                dataType:"JSON",
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success:function(res){
                    layer.close(loading);  //返回数据关闭loading
                    if(res.code == 0){
                        $("#saveBtn").removeClass("layui-btn-normal").addClass("layui-btn-disabled");
                        $("#saveBtn").attr("disabled","true");
                        layer.msg(res.message,{icon: 1},function(){
                            setTimeout('location.reload()',500);
                        });
                        
                    }
                    else{
                        layer.msg(res.message,{icon: 2});
                    }
                },
                error:function(e){
                    layer.msg("提交失败",{icon: 2});
                },
                
            });
            return false;
        });

        $(document).on('click','.new-day',function(){
            var value = $(this).data('value');
            var begin_time = '';
            var end_time = '';
            if(value == 7){
                begin_time = getDay(-7);
            }
            if(value == 30){
                begin_time = getDay(-30);
            }
            end_time = getDay(0);
            $("#begin_time").val(begin_time);
            $("#end_time").val(end_time);
            return false;
        })

        //获取当前日期
        function getDay(day){
            var today = new Date();
            var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
            today.setTime(targetday_milliseconds); //注意，这行是关键代码
            var tYear = today.getFullYear();
            var tMonth = today.getMonth();
            var tDate = today.getDate();
            tMonth = doHandleMonth(tMonth + 1);
            tDate = doHandleMonth(tDate);
            return tYear+"-"+tMonth+"-"+tDate;
        }
        function doHandleMonth(month){
            var m = month;
            if(month.toString().length == 1){
            m = "0" + month;
            }
            return m;
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            
            //执行搜索重载
            table.reload('listReload', {
                where: {
                    nickname: $("#nickname").val(),
                    name: $("#name").val(),
                    mobile: $("#mobile").val(),
                    order_no: $("#order_no").val(),
                    begin_time: $("#begin_time").val(),
                    end_time: $("#end_time").val(),
                    refund_status: $("#refund_status").val(),
                }
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            $("#nickname").val('');
            $("#name").val('');
            $("#mobile").val('');
            $("#order_no").val('');
            $("#begin_time").val('');
            $("#end_time").val('');
            $("#refund_status").val('');
            form.render();
        });

    });
</script>
@endsection
